package reflect;

import java.util.Scanner;

public class ReflectDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        //1.获取要实例化对象所对应类的类对象
        Class c = Class.forName("reflect.Person");
        //2.Class类的newInstance方法可以实例化对象
        Object obj = c.newInstance();//new Person();
        System.out.println(obj);

        Scanner scan = new Scanner(System.in);
        System.out.println("请输入一个类名：");
        String name = scan.nextLine();
        Class cls = Class.forName(name);
        Object obj2 = cls.newInstance();
        System.out.println(obj2);
        System.out.println(obj2.getClass().getName());
    }
}
